﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;

public partial class login1 : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LinkButton1.Text = createValidate(4);
        }
    }
    private string createValidate(int count)
    {
        string s1 = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
        string[] s1array = s1.Split(',');
        string randomcode = "";
        int temp = -1;
        Random rand = new Random();
        for (int i = 0; i < count; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(61);
            if (temp == t)
            {
                return createValidate(count);
            }
            temp = t;
            randomcode += s1array[t];
        }
        Session["valid"] = randomcode;
        return randomcode;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string pass = Encrypt(TextBox2.Text, "litianping");
        if (TextBox3.Text.ToUpper() == LinkButton1.Text.ToUpper() || TextBox3.Text.ToLower() == LinkButton1.Text.ToLower())
        {
            if (db.login(TextBox1.Text,pass) == 1)
            {
                Session["ID"] = db.phone_userName(TextBox1.Text);
                Response.Redirect("main1.aspx?id=00");
                //Response.Write(Session["ID"]);
            }
            else Response.Write("<script>alert('用户名或密码有误！')</script>");
        }
        else Response.Write("<script>alert('验证码错误！')</script>");
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton1.Text = createValidate(4);
    }
    public static string Encrypt(string Text, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputByteArray;
        inputByteArray = Encoding.Default.GetBytes(Text);
        des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
        des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        StringBuilder ret = new StringBuilder();
        foreach (byte b in ms.ToArray())
        {
            ret.AppendFormat("{0:X2}", b);
        }
        return ret.ToString();
    }
}